package org.jeecg.modules.commodity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.commodity.model.RelationshipModel;
import org.jeecg.modules.commodity.service.RelationShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Auther: zhuzhuhong
 * @Date: 2019/11/14 09:28
 * @Description:
 */
@RestController
@Log4j2
@RequestMapping(value = "/commodity/relationship")
public class RelationShipController {

    @Autowired
    private RelationShipService relationShipService;

    @GetMapping(value = "/list")
    public Result<IPage<RelationshipModel>> getRelationShip(
            @RequestParam("typeId") String typeId,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize
    ) {
        Result<IPage<RelationshipModel>> pageResult = new Result<>();
        try {
            IPage<RelationshipModel> page = new Page<>(pageNo,pageSize);
            List<RelationshipModel> relationShipModel = relationShipService.findRelationShipModelByPage(typeId);
            if (null != relationShipModel) {
                page.setRecords(relationShipModel);
                pageResult.setResult(page);
            } else {
                pageResult.error500("数据为空");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            pageResult.error500("后台异常");
        }
        return pageResult;
    }

    @PostMapping(value = "/saveList")
    public Result<Object> saveRelationShip(@RequestBody Map<String, String> paramMap) {
        Result<Object> result = new Result<>();
        try {
            boolean saveResult = relationShipService.saveRelationShip(paramMap);
            if (!saveResult) {
                result = Result.error("操作失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("后台异常");
        }
        return result;
    }

    @DeleteMapping(value = "del")
    public Result<Object> deletRelationShip(String id) {
        Result<Object> result = new Result<>();
        boolean removeResult = relationShipService.removeById(id);
        return result;
    }

}
